<%@ page import="java.io.InputStream" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.UUID" %><%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2022/8/25
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <%
      //都是Null
//      request.setCharacterEncoding("utf-8");
//      String title=request.getParameter("title");
//      String options[]=request.getParameterValues("options");
//      String file=request.getParameter("picture");
//      InputStream inputStream = request.getInputStream();
//      int ch=0;
//      while ((ch=inputStream.read())!=-1){
//          out.print((char)ch);
//      }
      DiskFileItemFactory df=new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(df);
      upload.setHeaderEncoding("utf-8");
      List<FileItem> fileItems = upload.parseRequest(request);
      for(FileItem f:fileItems){
          out.print("<h1>");
          if(f.isFormField()){//表单域
                out.print(f.getFieldName()+",值的是"+f.getString("utf-8"));
          }else{
              if(f.getSize()<1)
                  continue;
              out.println("文件名是"+f.getName());
              //>>>f.get();
              //>>>>byte[] bytes = f.get();

              String filePath=application.getRealPath("images");
              File folder=new File(filePath);
              if(!folder.exists())
                  folder.mkdirs();
              String picName=f.getName();
              String houzhui=picName.substring(picName.lastIndexOf('.'));
              picName= UUID.randomUUID()+houzhui;
              f.write(new File(folder,picName));
              out.print("<img src='images/"+picName+"' height='400px' width='500px'>");
          }
          out.print("</h1>");
      }
  %>
<%--<h1><%=title%></h1>
<%
    for(int i=0;options!=null&&i<options.length;i++)
        out.print("<h2>"+options[i]+"</h2>");
%>
<h1>文件名:<%=file%></h1>--%>
</body>
</html>
